package es.udc.pfc.davidparedes.web.pages.user;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

import com.google.gson.Gson;

import es.udc.pfc.davidparedes.model.shift.Shift;
import es.udc.pfc.davidparedes.model.storeservice.StoreService;
import es.udc.pfc.davidparedes.model.task.Task;
import es.udc.pfc.davidparedes.model.universe.Universe;
import es.udc.pfc.davidparedes.model.universemanager.UniverseManager;
import es.udc.pfc.davidparedes.model.userprofile.UserProfile;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.GlobalNames;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.CalendarEvent;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.MANAGER_OR_ABOVE)
public class UniverseSchedule {
	
	@Property
	private UserProfile userProfile;
	
	@Property
	private Universe universe;
	
	@Property
    private List<Task> tasks;
    
    @Property
    private List<Shift> shifts;
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private UserService userService;
    
    @Inject
    private StoreService storeService;
    
    @Environmental
    private JavaScriptSupport javaScriptSupport;
    
    void onActivate(Long id) throws InstanceNotFoundException {
		userProfile = userService.findUserProfile(userSession.getUserProfileId());
		if (userProfile instanceof UniverseManager) {
			UniverseManager manager = (UniverseManager) userProfile;
			universe = manager.getUniverse();
		} else {
			universe = storeService.findUniverse(id);
		}
		
		if (universe != null) {
			Calendar startMonth = Calendar.getInstance();
			startMonth.add(Calendar.MONTH, GlobalNames.PREVIOUS_LOADED_MONTHS);
			startMonth.set(Calendar.DAY_OF_MONTH, 1);
			startMonth.set(Calendar.HOUR_OF_DAY, 0);
			startMonth.set(Calendar.MINUTE, 0);
			startMonth.set(Calendar.SECOND, 0);
			
			Calendar endMonth = Calendar.getInstance();
			endMonth.add(Calendar.MONTH, GlobalNames.FOLLOWING_LOADED_MONTHS);
			int maxDay = endMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
			endMonth.set(Calendar.DAY_OF_MONTH, maxDay);
			endMonth.set(Calendar.HOUR_OF_DAY, 23);
			endMonth.set(Calendar.MINUTE, 59);
			endMonth.set(Calendar.SECOND, 59);
			
			tasks = storeService.getUniverseTasksByDate(universe.getUniverseId(), startMonth, endMonth);
			shifts = storeService.getUniverseShiftsByDate(universe.getUniverseId(), startMonth, endMonth);
		}
		
    }
    
    private String tasksToJSON(){
    	ArrayList<CalendarEvent> array = new ArrayList<CalendarEvent>();
    	CalendarEvent event;
    	for(Task task : tasks){
    		if ((task.getType() != GlobalNames.TASK_VACATION)
    				&& (task.getType() != GlobalNames.TASK_LOW)
    				&& (task.getType() != GlobalNames.TASK_ABSENTEEISM)) {
    			event = new CalendarEvent();
		        event.setId(task.getTaskId());
		        event.setTitle(task.getVendor().getFirstName()+" "+task.getVendor().getLastName()+" ("+task.getType()+")");
		        event.setAllDay(false);
		        event.setStart(task.getStart().getTime());
		        event.setEnd(task.getEnd().getTime());
		        event.setBackgroundColor(GlobalNames.COLOR_ORANGE);
		        event.setBorderColor(GlobalNames.COLOR_GRAY);
		        event.setUrl("/schedule-manager/user/updatetask/" + task.getTaskId());
		        event.setTarget("#updateTask");
		        array.add(event);
    		}
    	}
    	
    	for(Shift shift : shifts){
    		event = new CalendarEvent();
	        event.setId(shift.getShiftId());
	        event.setTitle(shift.getManager().getFirstName()+" "+shift.getManager().getLastName());
	        event.setAllDay(false);
	        event.setStart(shift.getStart().getTime());
	        event.setEnd(shift.getEnd().getTime());
	        event.setBackgroundColor(GlobalNames.COLOR_BLUE);
	        event.setBorderColor(GlobalNames.COLOR_GRAY);
	        array.add(event);
    	}
    	
    	Gson gson = new Gson();
	    String json = gson.toJson(array);
	    return json;
    	
    }
    
    Long onPassivate() {
    	return universe.getUniverseId();
    }
    
    @AfterRender
    public void afterRender() {
    	if (universe != null) {
    		javaScriptSupport.addScript("loadCalendar(%s)", tasksToJSON());
    	}
    }
    
}
